/**
 * 格式化时间戳
 */
function formatDate(inputTime, type) {
  type = type || 1;
  let returnVal;
  var date = new Date(inputTime);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  var minute = date.getMinutes();
  minute = minute < 10 ? ('0' + minute) : minute;
  switch (type) {
    case 1:
      returnVal = y + '-' + m + '-' + d;
      break;// 返回年月日
    case 2:
      returnVal = y + '-' + m + '-' + d + ' ' + h + ':' + minute;
      break;// 返回年月日时分
    case 3:
      returnVal = y + '年' + m + '月' + d + '日 ';
      break;// 返回年月日
    case 4:
      returnVal = h + ':' + minute;
  }
  return returnVal
}

/**
 * 显示失败提示框
 */
function showError(title, msg, cancelText,confirmText,callback,failback) {
  wx.getNetworkType({
    success: (res) => {
      msg = res.networkType == 'none' ? '网络出小差了，很快回来' : msg;
      wx.showModal({
        title: title,
        content: msg,
        cancelText: cancelText ? cancelText :'取消',
        confirmText: confirmText ? confirmText :'确定',
        showCancel: true,
        success: function (res) {
          if(res.cancel){
            failback && failback();
          }
          else{
            callback && callback();
          }
          
        },
        fail:function(res){}
      });
    }
  })
}

module.exports = {
  formatDate,
  showError
}
